<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
class CI_Output
{
	/**
	 * Final output string
	 *
	 * @var	string
	 */
	public $final_output;
	/**
	 * Cache expiration time
	 *
	 * @var	int
	 */
	public $cache_expiration = 0;
	/**
	 * List of server headers
	 *
	 * @var	array
	 */
	public $headers = array();
	/**
	 * List of mime types
	 *
	 * @var	array
	 */
	public $mimes = array();
	/**
	 * Mime-type for the current page
	 *
	 * @var	string
	 */
	protected $mime_type = 'text/html';
	/**
	 * Enable Profiler flag
	 *
	 * @var	bool
	 */
	public $enable_profiler = false;
	/**
	 * php.ini zlib.output_compression flag
	 *
	 * @var	bool
	 */
	protected $_zlib_oc = false;
	/**
	 * CI output compression flag
	 *
	 * @var	bool
	 */
	protected $_compress_output = false;
	/**
	 * List of profiler sections
	 *
	 * @var	array
	 */
	protected $_profiler_sections = array();
	/**
	 * Parse markers flag
	 *
	 * Whether or not to parse variables like {elapsed_time} and {memory_usage}.
	 *
	 * @var	bool
	 */
	public $parse_exec_vars = true;
	/**
	 * mbstring.func_overload flag
	 *
	 * @var	bool
	 */
	static protected $func_overload;

	public function __construct()
	{
		$this->_zlib_oc = (bool) ini_get('zlib.output_compression');
		$this->_compress_output = ($this->_zlib_oc === false) && (config_item('compress_output') === true) && extension_loaded('zlib');
		isset(self::$func_overload) || (self::$func_overload = extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
		$this->mimes = &get_mimes();
		log_message('info', 'Output Class Initialized');
	}

	public function get_output()
	{
		return $this->final_output;
	}

	public function set_output($output)
	{
		$this->final_output = $output;
		return $this;
	}

	public function append_output($output)
	{
		$this->final_output .= $output;
		return $this;
	}

	public function set_header($header, $replace = true)
	{
		if ($this->_zlib_oc && (strncasecmp($header, 'content-length', 14) === 0)) {
			return $this;
		}

		$this->headers[] = array($header, $replace);
		return $this;
	}

	public function set_content_type($mime_type, $charset = NULL)
	{
		if (strpos($mime_type, '/') === false) {
			$extension = ltrim($mime_type, '.');

			if (isset($this->mimes[$extension])) {
				$mime_type = &$this->mimes[$extension];

				if (is_array($mime_type)) {
					$mime_type = current($mime_type);
				}
			}
		}

		$this->mime_type = $mime_type;

		if (empty($charset)) {
			$charset = config_item('charset');
		}

		$header = 'Content-Type: ' . $mime_type . (empty($charset) ? '' : '; charset=' . $charset);
		$this->headers[] = array($header, true);
		return $this;
	}

	public function get_content_type()
	{
		$i = 0;

		for ($c = count($this->headers); $i < $c; $i++) {
			if (sscanf($this->headers[$i][0], 'Content-Type: %[^;]', $content_type) === 1) {
				return $content_type;
			}
		}

		return 'text/html';
	}

	public function get_header($header)
	{
		$headers = array_merge(array_map('array_shift', $this->headers), headers_list());
		if (empty($headers) || empty($header)) {
			return NULL;
		}

		for ($c = count($headers) - 1; -1 < $c; $c--) {
			if (strncasecmp($header, $headers[$c], $l = self::strlen($header)) === 0) {
				return trim(self::substr($headers[$c], $l + 1));
			}
		}

		return NULL;
	}

	public function set_status_header($code = 200, $text = '')
	{
		set_status_header($code, $text);
		return $this;
	}

	public function enable_profiler($val = true)
	{
		$this->enable_profiler = is_bool($val) ? $val : true;
		return $this;
	}

	public function set_profiler_sections($sections)
	{
		if (isset($sections['query_toggle_count'])) {
			$this->_profiler_sections['query_toggle_count'] = (int) $sections['query_toggle_count'];
			unset($sections['query_toggle_count']);
		}

		foreach ($sections as $section => $enable) {
			$this->_profiler_sections[$section] = $enable !== false;
		}

		return $this;
	}

	public function cache($time)
	{
		$this->cache_expiration = is_numeric($time) ? $time : 0;
		return $this;
	}

	public function _display($output = '')
	{
		$BM = &load_class('Benchmark', 'core');
		$CFG = &load_class('Config', 'core');

		if (class_exists('CI_Controller', false)) {
			$CI = &get_instance();
		}

		if ($output === '') {
			$output = &$this->final_output;
		}

		if ((0 < $this->cache_expiration) && isset($CI) && !method_exists($CI, '_output')) {
			$this->_write_cache($output);
		}

		$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');

		if ($this->parse_exec_vars === true) {
			$memory = round(memory_get_usage() / 1024 / 1024, 2) . 'MB';
			$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
		}

		if (isset($CI) && ($this->_compress_output === true) && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) {
			ob_start('ob_gzhandler');
		}

		if (0 < count($this->headers)) {
			foreach ($this->headers as $header) {
				@header($header[0], $header[1]);
			}
		}

		if (!isset($CI)) {
			if ($this->_compress_output === true) {
				if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) {
					header('Content-Encoding: gzip');
					header('Content-Length: ' . self::strlen($output));
				}
				else {
					$output = gzinflate(self::substr($output, 10, -8));
				}
			}

			echo $output;
			log_message('info', 'Final output sent to browser');
			log_message('debug', 'Total execution time: ' . $elapsed);
			return NULL;
		}

		if ($this->enable_profiler === true) {
			$CI->load->library('profiler');

			if (!empty($this->_profiler_sections)) {
				$CI->profiler->set_sections($this->_profiler_sections);
			}

			$output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count) . $CI->profiler->run();

			if (0 < $count) {
				$output .= '</body></html>';
			}
		}

		if (method_exists($CI, '_output')) {
			$CI->_output($output);
		}
		else {
			echo $output;
		}

		log_message('info', 'Final output sent to browser');
		log_message('debug', 'Total execution time: ' . $elapsed);
	}

	public function _write_cache($output)
	{
		$CI = &get_instance();
		$path = $CI->config->item('cache_path');
		$cache_path = ($path === '' ? APPPATH . 'cache/' : $path);
		if (!is_dir($cache_path) || !is_really_writable($cache_path)) {
			log_message('error', 'Unable to write cache file: ' . $cache_path);
			return NULL;
		}

		$uri = $CI->config->item('base_url') . $CI->config->item('index_page') . $CI->uri->uri_string();
		if (($cache_query_string = $CI->config->item('cache_query_string')) && !empty($_SERVER['QUERY_STRING'])) {
			if (is_array($cache_query_string)) {
				$uri .= '?' . http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));
			}
			else {
				$uri .= '?' . $_SERVER['QUERY_STRING'];
			}
		}

		$cache_path .= md5($uri);

		if (!($fp = @fopen($cache_path, 'w+b'))) {
			log_message('error', 'Unable to write cache file: ' . $cache_path);
			return NULL;
		}

		if (flock($fp, LOCK_EX)) {
			if ($this->_compress_output === true) {
				$output = gzencode($output);

				if ($this->get_header('content-type') === NULL) {
					$this->set_content_type($this->mime_type);
				}
			}

			$expire = time() + ($this->cache_expiration * 60);
			$cache_info = serialize(array('expire' => $expire, 'headers' => $this->headers));
			$output = $cache_info . 'ENDCI--->' . $output;
			$written = 0;

			for ($length = self::strlen($output); $written < $length; $written += $result) {
				if (($result = fwrite($fp, self::substr($output, $written))) === false) {
					break;
				}
			}

			flock($fp, LOCK_UN);
		}
		else {
			log_message('error', 'Unable to secure a file lock for file at: ' . $cache_path);
			return NULL;
		}

		fclose($fp);

		if (is_int($result)) {
			chmod($cache_path, 416);
			log_message('debug', 'Cache file written: ' . $cache_path);
			$this->set_cache_header($_SERVER['REQUEST_TIME'], $expire);
		}
		else {
			@unlink($cache_path);
			log_message('error', 'Unable to write the complete cache content at: ' . $cache_path);
		}
	}

	public function _display_cache(&$CFG, &$URI)
	{
		$cache_path = ($CFG->item('cache_path') === '' ? APPPATH . 'cache/' : $CFG->item('cache_path'));
		$uri = $CFG->item('base_url') . $CFG->item('index_page') . $URI->uri_string;
		if (($cache_query_string = $CFG->item('cache_query_string')) && !empty($_SERVER['QUERY_STRING'])) {
			if (is_array($cache_query_string)) {
				$uri .= '?' . http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));
			}
			else {
				$uri .= '?' . $_SERVER['QUERY_STRING'];
			}
		}

		$filepath = $cache_path . md5($uri);
		if (!file_exists($filepath) || !($fp = @fopen($filepath, 'rb'))) {
			return false;
		}

		flock($fp, LOCK_SH);
		$cache = (0 < filesize($filepath) ? fread($fp, filesize($filepath)) : '');
		flock($fp, LOCK_UN);
		fclose($fp);

		if (!preg_match('/^(.*)ENDCI--->/', $cache, $match)) {
			return false;
		}

		$cache_info = unserialize($match[1]);
		$expire = $cache_info['expire'];
		$last_modified = filemtime($filepath);
		if (($expire <= $_SERVER['REQUEST_TIME']) && is_really_writable($cache_path)) {
			@unlink($filepath);
			log_message('debug', 'Cache file has expired. File deleted.');
			return false;
		}
		else {
			$this->set_cache_header($last_modified, $expire);
		}

		foreach ($cache_info['headers'] as $header) {
			$this->set_header($header[0], $header[1]);
		}

		$this->_display(self::substr($cache, self::strlen($match[0])));
		log_message('debug', 'Cache file is current. Sending it to browser.');
		return true;
	}

	public function delete_cache($uri = '')
	{
		$CI = &get_instance();
		$cache_path = $CI->config->item('cache_path');

		if ($cache_path === '') {
			$cache_path = APPPATH . 'cache/';
		}

		if (!is_dir($cache_path)) {
			log_message('error', 'Unable to find cache path: ' . $cache_path);
			return false;
		}

		if (empty($uri)) {
			$uri = $CI->uri->uri_string();
			if (($cache_query_string = $CI->config->item('cache_query_string')) && !empty($_SERVER['QUERY_STRING'])) {
				if (is_array($cache_query_string)) {
					$uri .= '?' . http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));
				}
				else {
					$uri .= '?' . $_SERVER['QUERY_STRING'];
				}
			}
		}

		$cache_path .= md5($CI->config->item('base_url') . $CI->config->item('index_page') . ltrim($uri, '/'));

		if (!@unlink($cache_path)) {
			log_message('error', 'Unable to delete cache file for ' . $uri);
			return false;
		}

		return true;
	}

	public function set_cache_header($last_modified, $expiration)
	{
		$max_age = $expiration - $_SERVER['REQUEST_TIME'];
		if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($last_modified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
			$this->set_status_header(304);
			exit();
		}
		else {
			header('Pragma: public');
			header('Cache-Control: max-age=' . $max_age . ', public');
			header('Expires: ' . gmdate('D, d M Y H:i:s', $expiration) . ' GMT');
			header('Last-modified: ' . gmdate('D, d M Y H:i:s', $last_modified) . ' GMT');
		}
	}

	static protected function strlen($str)
	{
		return self::$func_overload ? mb_strlen($str, '8bit') : strlen($str);
	}

	static protected function substr($str, $start, $length = NULL)
	{
		if (self::$func_overload) {
			isset($length) || ($length = (0 <= $start ? self::strlen($str) - $start : 0 - $start));
			return mb_substr($str, $start, $length, '8bit');
		}

		return isset($length) ? substr($str, $start, $length) : substr($str, $start);
	}
}

defined('BASEPATH') || exit('No direct script access allowed');

?>
